<?php

namespace app\service\utils;


use OSS\Core\OssException;
use OSS\OssClient;

class OssService
{
    protected $config;

    public function __construct()
    {
        $this->config = config('oss.oss_config');
    }

    public function uploadImage($fileName, $filePath)
    {
        $accessKeyId = $this->config['access_key_id'];
        $accessKeySecret = $this->config['access_key_secret'];
        $endpoint = $this->config['endpoint'];
        $bucket = $this->config['bucket'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $result = $ossClient->uploadFile($bucket, $fileName, $filePath);
            return $result['oss-request-url'];
        } catch (OssException $e) {
            throw new \Exception($e->getMessage(), $e->getCode());
        }
    }
}
